<?php

class ModelNewsNews extends Model {

    public function addnews($data) {
		$this->load->model('tool/seo_url');
        $this->db->query("INSERT INTO " . DB_PREFIX . "news SET sort_order = '" . (int) $this->request->post['sort_order'] . "', category_news_id = 0, date_added = NOW()");


        $news_id = $this->db->getLastId();
        foreach ($data['news_description'] as $language_id => $value) {

            $this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int) $news_id . "', language_id = '" . (int) $language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "',seo_name='" . $this->seo_name('news_description', $this->model_tool_seo_url->khongdau($value['title'])) . "'");
        }

        if (isset($data['news_category'])) {
            $i = 0;
            foreach ($data['news_category'] as $category_news_id) {
                $i++;
                $this->db->query("INSERT INTO " . DB_PREFIX . "news_to_category SET news_id = '" . (int) $news_id . "', category_news_id = '" . (int) $category_news_id . "'");

                if ($i == sizeof($data['news_category'])) {
                    $this->db->query("UPDATE " . DB_PREFIX . "news SET category_news_id = '" . (int) $category_news_id . "' WHERE news_id = '" . (int) $news_id . "'");
                }
            }
        }
        $this->cache->delete('news');
    }

    public function seo_name($table, $title) {
        $query = $this->db->query("SELECT * FROM ".DB_PREFIX.$table." WHERE seo_name LIKE '" . $title . "'");
        if ($query->num_rows > 0) {
            $index = $query->num_rows;
            $title = $title . '-' . ++$index;
        }
        return $title;
    }

    public function editnews($news_id, $data) {

        $this->load->model('tool/seo_url');

        $this->db->query("UPDATE " . DB_PREFIX . "news SET sort_order = '" . (int) $data['sort_order'] . "' WHERE news_id = '" . (int) $news_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int) $news_id . "'");

        foreach ($data['news_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int) $news_id . "', language_id = '" . (int) $language_id . "', title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "',seo_name='" . $this->seo_name('news_description', $this->model_tool_seo_url->khongdau($value['title'])) . "'");
        }


        $this->db->query("DELETE FROM " . DB_PREFIX . "news_to_category WHERE news_id = '" . (int) $news_id . "'");

        if (isset($data['news_category'])) {
            $i = 0;
            foreach ($data['news_category'] as $category_news_id) {
                $i++;
                $this->db->query("INSERT INTO " . DB_PREFIX . "news_to_category SET news_id = '" . (int) $news_id . "', category_news_id = '" . (int) $category_news_id . "'");

                if ($i == sizeof($data['news_category'])) {
                    $this->db->query("UPDATE " . DB_PREFIX . "news SET category_news_id = '" . (int) $category_news_id . "' WHERE news_id = '" . (int) $news_id . "'");
                }
            }
        }


        $this->cache->delete('news');
    }

    public function getnewsCategories($news_id) {
        $news_category_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_to_category WHERE news_id = '" . (int) $news_id . "'");

        foreach ($query->rows as $result) {
            $news_category_data[] = $result['category_news_id'];
        }

        return $news_category_data;
    }

    public function getnewssByCategoryId($category_news_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news p LEFT JOIN " . DB_PREFIX . "news_description pd ON (p.news_id = pd.news_id) LEFT JOIN " . DB_PREFIX . "news_to_category p2c ON (p.news_id = p2c.news_id) WHERE pd.language_id = '" . (int) $this->config->get('config_language_id') . "' AND p2c.category_news_id = '" . (int) $category_news_id . "' ORDER BY pd.name ASC");

        return $query->rows;
    }

    public function deletenews($news_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = '" . (int) $news_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int) $news_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "news_to_category WHERE news_id = '" . (int) $news_id . "'");

        $this->cache->delete('news');
    }

    public function getnews($news_id) {
        $query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "news WHERE news_id = '" . (int) $news_id . "'");

        return $query->row;
    }

    public function getnewss($data = array()) {
        if ($data) {

            //$sql = "SELECT *
            //		FROM ".DB_PREFIX."news i INNER JOIN ".DB_PREFIX."news_description id ON i.news_id=id.news_id
            //		WHERE id.language_id=".$this->config->get('config_language_id');

            $sql = "SELECT * FROM " . DB_PREFIX . "news i LEFT JOIN " . DB_PREFIX . "news_description id ON (i.news_id = id.news_id) WHERE id.language_id = '" . (int) $this->config->get('config_language_id') . "'";

            $sort_data = array(
                'i.sort_order',
                'id.title'
            );

            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
                $sql .= " ORDER BY " . $data['sort'];
            } else {
                $sql .= " ORDER BY i.sort_order";
            }

            if (isset($data['order']) && ($data['order'] == 'DESC')) {
                $sql .= " DESC";
            } else {
                $sql .= " ASC";
            }

            if (isset($data['start']) || isset($data['limit'])) {
                if ($data['start'] < 0) {
                    $data['start'] = 0;
                }

                if ($data['limit'] < 1) {
                    $data['limit'] = 20;
                }

                $sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
            }

            $query = $this->db->query($sql);
            return $query->rows;
        } else {
            $news_data = $this->cache->get('news.' . $this->config->get('config_language_id'));

            if (!$news_data) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news i LEFT JOIN " . DB_PREFIX . "news_description id ON (i.news_id = id.news_id) WHERE id.language_id = '" . (int) $this->config->get('config_language_id') . "' ORDER BY i.sort_order");

                $news_data = $query->rows;

                $this->cache->set('news.' . $this->config->get('config_language_id'), $news_data);
            }

            return $news_data;
        }
    }

    public function getnewsDescriptions($news_id) {
        $news_description_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int) $news_id . "'");

        foreach ($query->rows as $result) {
            $news_description_data[$result['language_id']] = array(
                'title' => $result['title'],
                'description' => $result['description']
            );
        }

        return $news_description_data;
    }

    public function getTotalnewss() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");

        return $query->row['total'];
    }

}

?>